package com.jing.cloud.service.chat.adapter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jing.cloud.service.chat.bean.Message;
import com.jing.cloud.service.chat.constant.ChatConstant;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * Created by 29017 on 2017/9/10.
 */
@Data
public class MessageBean {
    private Long id;
    private Long sender;
    private Long target;
    //    private String room;
//    private Long roomShard;
    private Integer scene;
    private Integer type;
    private Integer type2;
    private List<Long> limitUserList;
    private String ticket;
    private Integer deviceType;
    private Long receivedId;
    private Long localId;
    private JSONObject message;

    public static MessageBean newInstance(Message msg) {
        MessageBean messageBean = new MessageBean();
        BeanUtils.copyProperties(msg, messageBean);
        messageBean.setMessage(JSON.parseObject(msg.getData()));
        messageBean.setLimitUserList(JSON.parseArray(msg.getLimitUsers(), Long.class));
        return messageBean;
    }

    public Message convert2Message() {
        Message message = new Message();
        BeanUtils.copyProperties(this, message);
        String room;
        if (scene == ChatConstant.MESSAGE_SCENE_U2U) {
            long small = sender < target ? sender : target;
            long large = sender > target ? sender : target;
            room = small + "+" + large;
            message.setRoomShard(small + large);
        } else {
            room = "" + target;
            message.setRoomShard(target);
        }
        message.setRoom(room);
        message.setData(JSON.toJSONString(this.message));
        message.setLimitUsers(JSON.toJSONString(this.limitUserList));
        message.forCreate();
        return message;
    }
}
